package com.createidea.scrumfriend.struts.action;

import com.createidea.scrumfriend.service.user.UserService;
import com.createidea.scrumfriend.to.UserTo;

public class UserAction extends BaseAction {
	
	private UserTo user;
	private UserService userService;
	private String username;
	private String password;
	private String projectId;
	private boolean ok;
	private static final String REDIRECT_DEFAULT_PROJECT="redirect_default_project";
	private static final String REDIRECT_PROJECT_PAGE="redirect_project_page";
	
	public String verifyUser(){
		UserTo proposedUser=userService.getUserByName(user.getName());
		if(proposedUser!=null && proposedUser.getPassword().equals(user.getPassword())){
		   this.getSession().setAttribute(this.USER, proposedUser.getId());
		  
		   if(userService.hasDefaultProject(proposedUser))
			   return REDIRECT_DEFAULT_PROJECT;
		   else
		       return REDIRECT_PROJECT_PAGE;
		 
		}
		else
		{
			this.addActionMessage("Wrong User Name or Password!");
			return INPUT;
		}
	}

	public String setDefaultProject(){
		userService.setDefaultProject((String)this.getSession().getAttribute(this.USER),projectId);
		ok = true;
		return SUCCESS;
	}
	public String loadRegister(){
		return SUCCESS;
	}
	
	public String registerUser(){
		if(userService.registerUser(username,password))
		   return SUCCESS;
		else
			return INPUT;
	}
	
	public String getDefaultProject(){
		
		user=userService.getUserById((String)this.getSession().getAttribute(this.USER));
		if(user!=null)
			projectId=user.getDefaultProject().getId();
		return SUCCESS;
	}
	
	/*
	 * get and set methods 
	 */
	public void setOk(boolean ok) {
		this.ok = ok;
	}
	public boolean isOk() {
		return ok;
	}
	public UserTo getUser() {
		return user;
	}

	public void setUser(UserTo user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}
	public String getProjectId() {
		return projectId;
	}


	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

}
